<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\File;
use App\Radpostauth;
class TestController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    public function updatedate()
    {

        ini_set('max_execution_time', '0');


        $start_date = strtotime('2018-01-17 00:00:01');

        $end_date = strtotime('2018-01-17 24:59:59');



        for($i=$start_date;$i<=$end_date;$i++)
        {
            $j = strtotime('2018-01-31 18:06:06');

            $date = date('Y-m-d H:i:s',$j);
            $z = date('Y-m-d H:i:s',$i);

            $stauth =  Radpostauth::where('authdate',$date)->get();

            foreach ($stauth as $value)
            {
                $value->authdate = $z;
                $value->save();
            }

            $j++;
            echo $j.'<br>';

        }


    }


    function pingAddress(Request $request)
    {
        $address = $request->input('address');
        $status = -1;
        if (strcasecmp(PHP_OS, 'WINNT') === 0) {
            // Windows 服务器下
            $pingresult = exec("ping -n 1 {$address}", $outcome, $status);
        } elseif (strcasecmp(PHP_OS, 'Linux') === 0) {
            // Linux 服务器下
            $pingresult = exec("ping -c 1 {$address}", $outcome, $status);
        }

        //  radius 授权用户账户密码配置
//        $data = [
//
//            'DB_RADIUS_DATABASE' => 'radius',
//            'DB_RADIUS_USERNAME' => 'niumo777',
//            'DB_RADIUS_HOST' => '172.16.87.178',
//            'DB_RADIUS_PASSWORD' => 'niumo777',
//        ];

        $data = [

            'DB_RADIUS_DATABASE' => 'radius',
            'DB_RADIUS_USERNAME' => 'root',
            'DB_RADIUS_HOST' => 'localhost',
            'DB_RADIUS_PASSWORD' => '',
        ];



        if($status != 0)
        {

            //   修改env配置
            self::modifyEnv($data);

            //   修改二级域名解析地址

        }


    }



    //  修改ENV   配置文件
    static  function modifyEnv(array $data)
    {
        $envPath = base_path() . DIRECTORY_SEPARATOR . '.env';

        $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES));

        $contentArray->transform(function ($item) use ($data){
            foreach ($data as $key => $value){
                if(str_contains($item, $key)){
                    return $key . '=' . $value;
                }
            }

            return $item;
        });

        $content = implode($contentArray->toArray(), "\n");

        File::put($envPath, $content);
    }


    /*


DB_RADIUS_DATABASE=radius
DB_RADIUS_USERNAME=niumo
DB_RADIUS_HOST=172.16.87.178
DB_RADIUS_PASSWORD=niumoradius123
     *
     *
     */


}